$(function() {
	dateOption = {
		pickDate : true,
		pickTime : false,
		format : 'YYYY年MM月DD日',
		language : 'zh-CN',
	};
	$('#startTimePicker').datetimepicker(dateOption);
	$('#endTimePicker').datetimepicker(dateOption);

	var table = $('#data').DataTable({
		"processing" : true,
		"serverSide" : true,
		"dom" : 'tr<"bottom"ip>',
		"ajax" : {
			"url" : window.basePath + "dataHeat/monitorList",
			"type" : "POST",
		},
		"language" : {
			"zeroRecords" : "无数据！",
			"infoEmpty" : "没有找到该条记录！",
			"infoFiltered" : "(共 _MAX_ 条记录)",
			"processing" : "加载中...",
			"lengthMenu" : "_MENU_ Einträge anzeigen",
			"info" : "显示第_START_ 到第 _END_条记录 （共 _TOTAL_ 条记录）",
			"infoPostFix" : "",
			"paginate" : {
				"first" : "第一页",
				"previous" : "上一页",
				"next" : "下一页",
				"last" : "最后一页"
			}
		},
		"columns" : [ {
			"class" : 'details-control',
			"orderable" : false,
			"data" : null,
			"defaultContent" : ''
		}, {
			"data" : "projectName"
		}, {
			"data" : "deviceKey"
		}, {
			"data" : "building"
		}, {
			"data" : "door"
		}, {
			"data" : "user"
		}, {
			"data" : "fee"
		}, {
			"data" : "createtime"
		}, {
			"data" : null,
			"visible" : false,
			"orderable" : false
		}, {
			"data" : null,
			"visible" : false,
			"orderable" : false
		} ],
		"order" : [ [ 1, "asc" ] ]

	});
	$.fn.dataTable.ext.errMode = 'throw';
	$("#searchProjectName").on('keyup', function() {
		table.column(1).search(this.value).draw();
		$("#searchProjectId").val("");
		table.column(9).search("").draw();
	});

	$("#searchDeviceKey").on('keyup', function() {
		table.column(2).search(this.value).draw();
	});

	$("#searchBuilding").on('keyup', function() {
		table.column(3).search(this.value).draw();
	});
	$("#searchDoor").on('keyup', function() {
		table.column(4).search(this.value).draw();
	});
	$("#searchUser").on('keyup', function() {
		table.column(5).search(this.value).draw();
	});
	$("#searchStart").on('keyup', function() {
		table.column(7).search(this.value).draw();
	});
	$("#startTimePicker").on('dp.change', function(e) {
		table.column(7).search(e.date.format('YYYY-MM-DD')).draw();
		$('#endTimePicker').data("DateTimePicker").setMinDate(e.date);
	});
	$("#searchEnd").on('keyup', function() {
		table.column(8).search(this.value).draw();
	});
	$("#endTimePicker").on('dp.change', function(e) {
		table.column(8).search(e.date.format('YYYY-MM-DD')).draw();
		$('#startTimePicker').data("DateTimePicker").setMaxDate(e.date);
	});

	$('#data tbody').on('click', 'td.details-control', function() {
		var tr = $(this).parents('tr');
		var row = table.row(tr);

		if (row.child.isShown()) {
			// This row is already open - close it
			row.child.hide();
			tr.removeClass('shown');
		} else {
			// Open this row
			row.child(format(row.data())).show();
			tr.addClass('shown');
		}
	});

	function format(data) {
		return '<table  class="table"><tr><td>累计热能：</td><td><em>' + Math.round(data.allheat)
				+ ' kWh</em></td><td>累计流量：</td><td><em>' + Math.round(data.allflow)
				+ ' m<SUP>3</SUP></em></td><td>瞬时热量：</td><td><em>' + data.currentHeat
				+ ' kW</em></td><td>瞬时流量：</td><td><em>' + Math.round(data.currentflow * 1000) / 1000.0
				+ ' m<SUP>3</SUP>/h</em></td></tr><tr><td>上个分摊周期热量:</td><td><em>' + Math.round(data.lastheat * 100)
				/ 100.0 + ' kW</em></td>' + '<td>进水温度：</td><td><em>' + Math.round(data.inTemperature * 10) / 10.0
				+ ' ℃</em></td><td>回水温度：</td><td><em>' + Math.round(data.outTemperature * 10) / 10.0
				+ ' ℃</em><td>通讯状态：</td><td><em>' + data.status + '</em></td></tr></table>';
	}

	$("#tree").dynatree(
			{
				selectMode : 3,
				onClick : function(node) {
					if (!node.data.isFolder) {
						// alert("You activated " + node.data.title);
						$("#searchProjectName").val(
								node.getParent().getParent().getParent().data.title
										+ node.getParent().getParent().data.title + node.getParent().data.title
										+ node.data.title);
						$("#searchProjectId").val(node.data.key);
						$("#tree").hide();
						table.column(9).search(node.data.key).draw();
					}
				},
				initAjax : {
					url : window.basePath + "project/loadProject"
				}
			});

	$("#searchProjectName").click(function(event) {
		$("#tree").toggle();
		$(document).one("click", function() {// 对document绑定一个影藏Div方法
			$("#tree").hide();
		});
		event.stopPropagation();// 点击Button阻止事件冒泡到document
	});
	$("#tree").click(function(event) {
		event.stopPropagation();// 在Div区域内的点击事件阻止冒泡到document
	});

	$('#collapseOne').on('show.bs.collapse', function() {
		$("#arrow").removeClass("glyphicon glyphicon-chevron-down").addClass("glyphicon glyphicon-chevron-up");
	});
	$('#collapseOne').on('hide.bs.collapse', function() {
		$("#arrow").removeClass("glyphicon glyphicon-chevron-up").addClass("glyphicon glyphicon-chevron-down");
	});
});